﻿using Estsh.Core.Controllers;
using Estsh.Core.Model.Extend.Models;
using Estsh.Core.Model.Result;
using Estsh.Core.Models;
using Estsh.Core.Wms.IServices;
using Microsoft.AspNetCore.Mvc;
using SqlSugar.Extensions;

namespace Estsh.Core.Web.Plugin.Wms.Areas.wms.Controllers.OutStock
{
    [Route("wms/[controller]")]
    [ApiController]
    public class FinishShippingController: WmsBaseController
    {
        public IFinishShippingService service;
        public FinishShippingController(IFinishShippingService _service)
        {
            service= _service;
        }

        /// <summary>
        /// 获取销售发运单
        /// </summary>
        /// <returns></returns>
        [HttpPost("GetShipping")]
        public IActionResult GetShipping(Newtonsoft.Json.Linq.JObject jobj)
        { 
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                string orderNo = jobj["orderNo"].ToString().Trim();
                List<WmsOutstock_Extend> wmsOutstocks= service.GetWmsOutstocks(orderNo);
                result.Data= wmsOutstocks;
                result.Success= true;
            }
            catch (Exception ex) 
            { 
                result.Msg= ex.Message;
            }
            return Json(result);
        }

        /// <summary>
        /// 获取发运明细
        /// </summary>
        /// <param name="orderNo"></param>
        /// <returns></returns>
        [HttpPost("GetShippingDetail")]
        public IActionResult GetShippingDetail(string orderNo)
        {
            WmsResponseResult result= new WmsResponseResult();
            try
            {
                List<WmsOutstockDetail> details = service.GetWmsOutstockDetail(orderNo,0);
                result.Data= details;
                result.Success= true;
            }
            catch (Exception ex)
            {
                result.Msg= ex.Message;
            }
            return Json(result);
        }

        /// <summary>
        /// 查询扫描的箱条码是否存在
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        [HttpPost("GetCartonNo")]
        public IActionResult GetCartonNo(Newtonsoft.Json.Linq.JObject jobj)
        {
            string no = jobj["no"].ToString().Trim();
            string shippingNo = jobj["shippingNo"].ToString().Trim();
            int loginId = int.Parse(jobj["loginId"].ToString().Trim());
            WmsResponseResult result=new WmsResponseResult();
            try
            {
                List<SysStock> stocks = service.GetStocks(no, 50);
                if (stocks.Count == 0)
                {
                    List<SysStock> sysStocks = service.GetStocks(no, 100);
                    if (sysStocks.Count > 0)
                    {
                        result.Code = 0;//此条码已扫描
                    }
                    else
                    {
                        result.Code = 1;//请检查此条码是否存在
                    }
                }
                else
                {
                    //先查询扫描的箱条码对应的物料是否在此发运单中
                    List<WmsOutstockDetail> outstockDetails = service.GetOutstockDetails(shippingNo, no);
                    if (outstockDetails.Count == 0)
                    {
                        result.Code = -1;
                    }
                    else
                    {
                        //此发运明细的需求数量已经和已配数量相同，则不能再扫描箱条码
                        int resu = service.GetDetailQty(shippingNo, no);
                        if (resu == 1)
                        {
                            result.Code = 3;
                        }
                        else
                        {
                            //更改此条码状态为处理中
                            service.UpdateOutStockDetail(shippingNo, no, loginId);
                            result.Data = stocks;//获取到此条码
                            result.Code = 2;
                        }
                    }
                }

                result.Success = true;
            }
            catch (Exception ex)
            {
                result.Msg= ex.Message;
            }
            return Json(result);
        }

        /// <summary>
        /// 如果数量一致的话则扫描下一个箱条码，否则的话拆箱
        /// </summary>
        /// <param name="cartonNo"></param>
        /// <param name="qty"></param>
        /// <returns></returns>
        [HttpPost("GetQtys")]
        public IActionResult GetQtys(Newtonsoft.Json.Linq.JObject jobj)
        {
            string cartonNo = jobj["cartonNo"].ToString().Trim();
            int qty =int.Parse( jobj["qty"].ToString().Trim());
            int loginid =int.Parse( jobj["loginid"].ToString().Trim());
            //string rackNo = jobj["rackNo"].ToString().Trim();
            //bool pack = jobj["pack"].ObjToBool();
            string shippingNo = jobj["shippingNo"].ToString().Trim();

            WmsResponseResult result = new WmsResponseResult();
            try
            {
                List<SysStock> stocks = service.GetQtys(cartonNo, qty);
                if (stocks.Count > 0)
                {
                    //添加package，发运记录
                    string packageRackno= service.AddRackPackage(cartonNo, loginid,shippingNo);
                    //添加发运记录
                    //service.AddShippingLog(shippingNo, cartonNo, loginid, rackNo);
                    //修改状态为已发运,推荐库位清空
                    int resu= service.UpdateStatusAndLocate(cartonNo,shippingNo,qty,loginid);
                    if (resu == -1)
                    {
                        result.Code = -1;
                        result.Success = true;
                    }
                    else
                    {
                        result.Code = 0;
                        result.Data = packageRackno;
                        result.Success = true;
                    }
                    
                }
                else
                {
                    List<SysStock> stocks1 = service.GetStocks(cartonNo,50);
                    if (stocks1.FirstOrDefault().Qty < qty.ObjToDecimal())
                    {
                        result.Code = 2;
                        result.Data = "错误";
                        result.Success = true;
                    }
                    if (stocks1.FirstOrDefault().Qty > qty.ObjToDecimal())
                    {
                        SetObjectDetail obj = service.CheckMaterialSplit(cartonNo, qty, loginid.ToString(), 1,shippingNo);
                        result.Code = 1;
                        result.Success = true;
                        result.Data = obj;

                    }
                }
            }
            catch (Exception ex)
            {
                result.Msg= ex.Message;
            }
            return Json(result);
        }

        /// <summary>
        /// 打包后判断单据是否完成
        /// </summary>
        /// <param name="jobj"></param>
        /// <returns></returns>
        [HttpPost("Packing")]
        public IActionResult Packing(Newtonsoft.Json.Linq.JObject jobj)
        {
            string weight = jobj["weight"].ToString().Trim();
            //string rackNo = jobj["rackNo"].ToString().Trim();
            string orderNo = jobj["orderNo"].ToString().Trim();
            string loginId = jobj["loginId"].ToString().Trim();

            WmsResponseResult result= new WmsResponseResult();
            try
            {
                //更改重量和运费
                service.UpdateShippingLog(orderNo, weight, loginId);

                //查询此发运单是否发运完成
                List<WmsOutstockDetail> details= service.GetWmsOutstockDetail(orderNo, 40);//已发运
                List<WmsOutstockDetail> details1 = service.GetWmsOutstockDetail(orderNo, 0);
                if (details.Count == details1.Count)
                {
                    //此发运单完成
                    service.UpdateOutStock(orderNo, loginId);
                    result.Code = 1;
                }
                result.Success = true;

            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }
        /// <summary>
        /// 判断是否有可打包的箱条码
        /// </summary>
        /// <param name="jobj"></param>
        /// <returns></returns>
        [HttpPost("GetPacks")]
        public IActionResult GetPacks(Newtonsoft.Json.Linq.JObject jobj)
        {
            string shippingNo = jobj["shippingNo"].ToString().Trim();
            WmsResponseResult result=new WmsResponseResult();
            try
            {
                List<SysShippingLog> shippingLogs=service.GetShippingLogs(shippingNo);
                if (shippingLogs.Count > 0)
                {
                    result.Code=1;
                }
                else
                {
                    result.Code=2;
                }
                result.Success = true;

            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        /// <summary>
        /// 判断单据是否完成
        /// </summary>
        /// <param name="jobj"></param>
        /// <returns></returns>
        [HttpPost("Submits")]
        public IActionResult Submits(Newtonsoft.Json.Linq.JObject jobj)
        {
            string orderNo = jobj["orderNo"].ToString().Trim();
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                //查询此发运单是否发运完成
                List<WmsOutstockDetail> details = service.GetWmsOutstockDetail(orderNo, 40);//已发运
                List<WmsOutstockDetail> details1 = service.GetWmsOutstockDetail(orderNo, 0);
                if (details.Count == details1.Count)
                {
                    result.Code=1;
                }
                else
                {
                    result.Code=2;
                }
                result.Success = true;
            }
            catch(Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }
    }
}
